package com.netx.generics.translation;
import com.netx.generics.basic.Checker;


public class BasicMessageFormatter implements MessageFormatter {

	private final String[] _stepNames;
	
	public BasicMessageFormatter() {
		this(null);
	}

	public BasicMessageFormatter(String[] stepNames) {
		_stepNames = stepNames;
		if(_stepNames != null) {
			Checker.checkEmptyElements(_stepNames, "stepNames");
		}
	}

	public String format(Message m) {
		StringBuilder message = new StringBuilder();
		// Message:
		if(m.getPosition() != null) {
			message.append("line ");
			message.append(m.getPosition().getLine()+": ");
			message.append(m.getMessage());
		}
		else {
			message.append(m.getMessage());
		}
		// Step name:
		if(_stepNames != null) {
			String stepName = _stepNames[m.getStepNumber()].trim();
			message.insert(0, ": ");
			message.insert(0, stepName);
		}
		return message.toString();
	}

}
